package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.StrategyCatalog;
import cn.wolfcode.luowowo.article.domain.StrategyContent;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.service.*;
import cn.wolfcode.luowowo.common.query.DestinationQueryObject;
import cn.wolfcode.luowowo.common.query.TravelQueryObject;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("destination")
public class DestinationController {

    @Reference
    private IRegionService regionService;
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IStrategyCatalogService catalogService;
    @Reference
    private IStrategyDetailService detailService;
    @Reference
    private ITravelService travelService;

    @RequestMapping("")
    public String index(Model model) {
        //查询热门区域
        model.addAttribute("hotRegions", regionService.listHotRegis());
        return "destination/index";
    }

    @RequestMapping("guide")
    public String guide(Long id, Model model) {
        //吐司
        List<Destination> toasts = destinationService.getToasts(id);
        model.addAttribute("toasts", toasts);
        model.addAttribute("dest", toasts.remove(toasts.size() - 1));//把删除的数据返回回来.

        //catalogs 分类,根据目的地查询所有分类.
        model.addAttribute("catalogs", catalogService.selectByDestId(id));

        //strategyDetails 该目的地下点击量前3的攻略
        model.addAttribute("strategyDetails", detailService.listViewnumTop3(id));

        return "destination/guide";
    }


    @RequestMapping("/listHotDestByRegionId")
    public String listHotDestByRegionId(@ModelAttribute("regionId") Long regionId, Model model) {
        List<Destination> list = destinationService.listHotDestByRegionId(regionId);

        List<Destination> leftDests = list.subList(0, list.size() / 2);
        List<Destination> rightDests = list.subList(list.size() / 2, list.size());
        model.addAttribute("leftDests", leftDests);
        model.addAttribute("rightDests", rightDests);
        return "destination/hotdestTpl";
    }

    @RequestMapping("surveyPage")
    public String surveyPage(@ModelAttribute("qo") DestinationQueryObject qo, Model model){
        //吐司
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
        model.addAttribute("toasts", toasts);
        model.addAttribute("dest", toasts.remove(toasts.size() - 1));//把删除的数据返回回来.

        return "destination/survey";
    }

    @RequestMapping("survey")
    public String survey(@ModelAttribute("qo") DestinationQueryObject qo,Model model){
    //根据目的地id查询   catalogs 分类
        List<StrategyCatalog> catalogs = catalogService.selectByDestId(qo.getDestId());
        model.addAttribute("catalogs",catalogs);
        //分类一下的文章和第一篇的内容
        for (StrategyCatalog catalog : catalogs) {
            if(catalog.getId().equals(qo.getCatalogId())){
                //选中当前的分类
                model.addAttribute("catalog",catalog);

                StrategyDetail detail = catalog.getDetails().get(0);
                StrategyContent content = detailService.getContent(detail.getId());
                detail.setStrategyContent(content);
                //共享第一篇文章内容
                model.addAttribute("detail",detail);
                break;
            }
        }

        return "destination/surveyTpl";
    }


    //目的地下的游记
    @RequestMapping("travels")
    public String travels(TravelQueryObject qo, Model model){
        //pageInfo
        model.addAttribute("pageInfo",travelService.query(qo));
        return "destination/travelTpl";
    }



}
